package com.sl.crawler.service.lianjia.house;

import com.sl.crawler.domain.House;
import com.sl.crawler.manager.HouseManager;
import com.sl.crawler.robot.output.IOutput;
import com.sl.crawler.robot.CrawlerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by shiling on 2017/5/26.
 */
@Service
@Lazy(false)
public class HouseMysqlOutputService implements IOutput {
    private final static Logger LOG = LoggerFactory.getLogger(HouseMysqlOutputService.class);

    @Autowired
    private HouseManager houseManager;


    @Override
    public void output(List<Map<String, String>> list, CrawlerConfig crawlerConfig) {
        List<House> xiaoquList = buildHouseList(list);
        saveOrUpdateHouseList(xiaoquList);
    }

    private void saveOrUpdateHouseList(List<House> houseList) {
        for (House house : houseList) {
            houseManager.saveOrUpdateHouseAndDetail(house);
        }
    }

    private static List<House> buildHouseList(List<Map<String, String>> resultList) {
        List<House> houseList = new ArrayList<>();
        for (Map<String, String> map : resultList) {
            House house = new House();
            house.setXiaoqu(map.get("xiaoqu"));
            house.setTitle(map.get("title"));
            house.setTotal(Float.valueOf(map.get("total")));
            house.setLocationTwo(map.get("location_two"));
            house.setHouseTag(map.get("house_tag"));
            house.setUnitPrice(getUnitPriceValue(map.get("unitPriceValue")));
            house.setHouseId(map.get("house_id"));
            house.setHouseTag(map.get("house_tag"));

            buildHouseInfo(map, house);

            buildPositionInfo(map, house);

            buildFollowInfo(map, house);

            houseList.add(house);
        }
        return houseList;
    }

    /**
     * 获取房屋信息
     *
     * @param map
     * @param house
     */
    private static void buildHouseInfo(Map<String, String> map, House house) {
        String regexPattern = "\\| (\\S*室\\S*) \\|(.*)平米 \\| (.*) \\| (.*)\\|?(.*电梯)?";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(map.get("house_info"));
        if (matcher.find()) {
            house.setHouseType(matcher.group(1));
            house.setHouseArea(Float.valueOf(matcher.group(2)));
            house.setHouseFacing(matcher.group(3));
            house.setDecorationSituation(matcher.group(4));
            house.setHaveElevator(matcher.group(5));
        }
    }

    /**
     * 获取位置信息
     *
     * @param map
     * @param house
     */
    private static void buildPositionInfo(Map<String, String> map, House house) {
        String regexPattern = "(.+楼层)\\(共(\\d+)层\\)(\\d+)年建(.+) -";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(map.get("position_info"));
        if (matcher.find()) {
            house.setHouseFloor(matcher.group(1));
            house.setTotalFloor(Integer.valueOf(matcher.group(2)));
            house.setBuildingYears(Integer.valueOf(matcher.group(3)));
            house.setBuildingType(matcher.group(4));
        }
    }


    /**
     * 获取带看信息
     *
     * @param map
     * @param house
     */
    private static void buildFollowInfo(Map<String, String> map, House house) {
        String regexPattern = "(\\d+)人关注 / 共(\\d+)次带看 / (.*)发布";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(map.get("follow_info"));
        if (matcher.find()) {
            house.setFollowCount(Integer.valueOf(matcher.group(1)));
            house.setLookCount(Integer.valueOf(matcher.group(2)));
            house.setPublishTime(matcher.group(3));
        }
    }

    /**
     * 单价55843元/平米 格式数据获取单价
     *
     * @param unitPriceValueStr
     * @return
     */
    private static Float getUnitPriceValue(String unitPriceValueStr) {
        String regexPattern = "\\d+";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(unitPriceValueStr);
        if (matcher.find()) {
            return Float.valueOf(matcher.group());
        }
        return 0f;
    }
}
